package com.example.controller.adm;

import com.example.pojo.Goods;
import com.example.service.IGoodsService;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import javax.xml.transform.Result;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 物品表 前端控制器
 * </p>
 * @author wsl
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/adm/gs")
@Api(tags = "物品管理",value = "")
@CrossOrigin(allowCredentials = "true")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @ApiOperation(value = "获取物品管理页面信息")
    @GetMapping("/query")
    public PageInfo<Goods> getGsPageInfo(Integer gsClassifyId,Integer enabled,String name,Integer num,
                                        @RequestParam(defaultValue = "1")Integer pageNum,
                                        @RequestParam(defaultValue = "3")Integer pageSize){
        return goodsService.selectGoods(gsClassifyId,enabled,name,num,pageNum,pageSize);
    }

    @GetMapping("/query2")
    @ApiOperation(value = "获取物品集合")
    public ResultVo getGsList() {
        List<Goods> list = goodsService.list();
        return ResultVo.response(list);
    }

    @ApiOperation(value = "获取物品所有信息")
    @GetMapping("/goodsAll")
    public List<Goods> goodsAll(){
        return goodsService.querygoods();
    }

    @PostMapping("/add")
    @ApiOperation(value ="新增物品")
    private ResultVo addGoods(@RequestBody Goods goods){
        if (goodsService.save(goods)) {
            return  ResultVo.success("新增成功");
        }
        return  ResultVo.error("新增失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    private ResultVo deleteGoodsById(Integer[] ids){
        return goodsService.removeByIds(Arrays.asList(ids)) ? ResultVo.success("删除成功") : ResultVo.error("删除失败");
    }

    @PutMapping("/update")
    @ApiOperation(value = "根据id修改")
    public ResultVo updateGoods(@RequestBody Goods goods){
        if(goodsService.updateById(goods)){
            return ResultVo.success("修改成功");
        }
        return ResultVo.error("修改失败");
    }

    @GetMapping("/has-same-code-gs")
    @ApiOperation(value = "是否存在有相同编码的物品")
    public Boolean hasSameCodeGs(Integer id, @RequestParam("code")String code){
        return goodsService.hasSameCodeGs(id,code);
    }
}
